/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.shop.service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.shop.dao.SpecificationDao;
import com.thinkgem.jeesite.modules.shop.dao.SpecificationItemDao;
import com.thinkgem.jeesite.modules.shop.entity.Specification;
import com.thinkgem.jeesite.modules.shop.entity.SpecificationItem;

/**
 * 商品规格管理Service
 * @author xxs
 * @version 2016-03-03
 */
@Service
@Transactional(readOnly = true)
public class SpecificationService extends CrudService<SpecificationDao, Specification> {

	@Autowired
	private SpecificationDao specificationDao;
	@Autowired
	private SpecificationItemDao specificationItemDao;
	
	public void filter(List<SpecificationItem> specificationItems) {
		CollectionUtils.filter(specificationItems, new Predicate() {
			public boolean evaluate(Object object) {
				SpecificationItem specificationItem = (SpecificationItem) object;
				if (specificationItem == null || StringUtils.isEmpty(specificationItem.getName())) {
					return false;
				}
				CollectionUtils.filter(specificationItem.getEntries(), new Predicate() {
					private Set<String> idSet = new HashSet<String>();
					private Set<String> valueSet = new HashSet<String>();

					public boolean evaluate(Object object) {
						SpecificationItem.Entry entry = (SpecificationItem.Entry) object;
						return entry != null && entry.getId() != null && StringUtils.isNotEmpty(entry.getValue()) && entry.getIsSelected() != null && idSet.add(entry.getId()) && valueSet.add(entry.getValue());
					}
				});
				return CollectionUtils.isNotEmpty(specificationItem.getEntries()) && specificationItem.isSelected();
			}
		});
	}
	
	public Specification get(String id) {
		Specification specification = super.get(id);
		specification.setSpecificationItemList(specificationItemDao.findList(new SpecificationItem(specification)));
		return specification;
	}
	public List<Specification> getSpecificationByCategoryId(String category_id) {
		return specificationDao.getSpecificationByCategoryId(category_id);
	}
	public List<Specification> findList(Specification specification) {
		return super.findList(specification);
	}
	
	public Page<Specification> findPage(Page<Specification> page, Specification specification) {
		return super.findPage(page, specification);
	}
	
	@Transactional(readOnly = false)
	public void save(Specification specification) {
		super.save(specification);
		for (SpecificationItem specificationItem : specification.getSpecificationItemList()){
			if (specificationItem.getId() == null){
				continue;
			}
			if (SpecificationItem.DEL_FLAG_NORMAL.equals(specificationItem.getDelFlag())){
				if (StringUtils.isBlank(specificationItem.getId())){
					specificationItem.setShopSpecification(specification);
					specificationItem.preInsert();
					specificationItemDao.insert(specificationItem);
				}else{
					specificationItem.preUpdate();
					specificationItemDao.update(specificationItem);
				}
			}else{
				specificationItemDao.delete(specificationItem);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(Specification specification) {
		super.delete(specification);
		specificationItemDao.delete(new SpecificationItem(specification));
	}
	
}